package com.jeethink.erp.transfer.service.impl;

import java.util.Date;
import java.util.List;

import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.erp.pile.domain.TbStockPile;
import com.jeethink.erp.pile.mapper.TbStockPileMapper;
import com.jeethink.erp.pileDetail.domain.TbStockPileDetail;
import com.jeethink.erp.pileDetail.mapper.TbStockPileDetailMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeethink.erp.transfer.mapper.TbStockTransferMapper;
import com.jeethink.erp.transfer.domain.TbStockTransfer;
import com.jeethink.erp.transfer.service.ITbStockTransferService;

/**
 * 库存调拨申请Service业务层处理
 * 
 * @author ypr
 * @date 2021-06-03
 */
@Service
public class TbStockTransferServiceImpl implements ITbStockTransferService 
{
    @Autowired
    private TbStockTransferMapper tbStockTransferMapper;

    @Autowired
    private TbStockPileMapper tbStockPileMapper;

    @Autowired
    private TbStockPileDetailMapper tbStockPileDetailMapper;

    /**
     * 查询库存调拨申请
     * 
     * @param transferId 库存调拨申请ID
     * @return 库存调拨申请
     */
    @Override
    public TbStockTransfer selectTbStockTransferById(Long transferId)
    {
        return tbStockTransferMapper.selectTbStockTransferById(transferId);
    }

    /**
     * 查询库存调拨申请列表
     * 
     * @param tbStockTransfer 库存调拨申请
     * @return 库存调拨申请
     */
    @Override
    public List<TbStockTransfer> selectTbStockTransferList(TbStockTransfer tbStockTransfer)
    {
        return tbStockTransferMapper.selectTbStockTransferList(tbStockTransfer);
    }

    /**
     * 新增库存调拨申请
     * 
     * @param tbStockTransfer 库存调拨申请
     * @return 结果
     */
    @Override
    public int insertTbStockTransfer(TbStockTransfer tbStockTransfer)
    {
        tbStockTransfer.setCreateTime(DateUtils.getNowDate());
        //新增是否同意调拨 默认2 未审批
        tbStockTransfer.setIsAgree("2");
        return tbStockTransferMapper.insertTbStockTransfer(tbStockTransfer);
    }

    /**
     * 修改库存调拨申请
     * 
     * @param tbStockTransfer 库存调拨申请
     * @return 结果
     */
    @Override
    public AjaxResult updateTbStockTransfer(TbStockTransfer tbStockTransfer)
    {
        tbStockTransfer.setUpdateTime(DateUtils.getNowDate());

        TbStockPile tbStockPile = new TbStockPile();
        tbStockPile.setShopId(tbStockTransfer.getShopId());
        tbStockPile.setCommodityId(tbStockTransfer.getCommodityId());
        //审批 如果审批为同意 新增库存操作
        if(tbStockTransfer.getIsAgree().equals("1")){
            //二级店铺信息
            TbStockPile congKu = tbStockPileMapper.selectCommodityId(tbStockPile);
            tbStockPile.setShop(tbStockPile.getShopId());
            tbStockPile.setShopId(null);
            tbStockPile.setIsMainLibrary("0");
            //根据商品id  店铺id 查询主库信息
            TbStockPile zhuKu = tbStockPileMapper.selectCommodityId(tbStockPile);
            //修改主库存数量
            int num = zhuKu.getPurchaseNum();
            int applyNum = Math.toIntExact(tbStockTransfer.getApplyNum());
            num = num - applyNum;
            //如果主库存数量为0 则无法添加库存
            if(num >= 0){

                zhuKu.setPurchaseNum(num);
                //出库时间
                zhuKu.setLastLeaveDate(new Date());
                tbStockPileMapper.updateTbStockPile(zhuKu);
            }else {
                return AjaxResult.error("审批失败 库存数量不足");
            }
            //新增库存明细
            TbStockPileDetail pileDetail = new TbStockPileDetail();
            pileDetail.setStockPileId(zhuKu.getStockPileId());
            pileDetail.setInventoryOperation("1");
            pileDetail.setOperationNum(tbStockTransfer.getApplyNum());
            pileDetail.setOperationTime(new Date());
            pileDetail.setCreateTime(new Date());
            tbStockPileDetailMapper.insertTbStockPileDetail(pileDetail);
            if(congKu != null){
                //如果有 则代表有库存信息  修改二级库存量
                int a = congKu.getPurchaseNum();
                int purchaseNum = a+applyNum;
                congKu.setPurchaseNum(purchaseNum);
                tbStockPileMapper.updateTbStockPile(congKu);

            }else{
                //是否为主库存 0 是 1 否
                tbStockPile.setIsMainLibrary("1");
                tbStockPile.setShopId(tbStockTransfer.getShopId());
                tbStockPile.setWarehouseId(zhuKu.getWarehouseId());
                tbStockPile.setFirstEnterDate(new Date());
                tbStockPile.setPurchaseNum(applyNum);
                //新增库存
                tbStockPileMapper.insertTbStockPile(tbStockPile);
            }
        }

        tbStockTransferMapper.updateTbStockTransfer(tbStockTransfer);


        return AjaxResult.success("审批通过");
    }

    /**
     * 批量删除库存调拨申请
     * 
     * @param transferIds 需要删除的库存调拨申请ID
     * @return 结果
     */
    @Override
    public int deleteTbStockTransferByIds(Long[] transferIds)
    {
        return tbStockTransferMapper.deleteTbStockTransferByIds(transferIds);
    }

    /**
     * 删除库存调拨申请信息
     * 
     * @param transferId 库存调拨申请ID
     * @return 结果
     */
    @Override
    public int deleteTbStockTransferById(Long transferId)
    {
        return tbStockTransferMapper.deleteTbStockTransferById(transferId);
    }

    @Override
    public List<TbStockTransfer> getMyTransferList(TbStockTransfer tbStockTransfer) {
        return tbStockTransferMapper.selectTbStockTransferList(tbStockTransfer);
    }
}
